import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'

// 定义路由记录的类型
const routes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: '首页',
    },
  },
] as const

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  // 使用可选链和类型断言来安全地访问meta属性
  document.title = `${(to.meta.title as string) || '默认标题'}`
  next()
})

export default router
